package com.azusa.xbiao.dao;

import com.azusa.xbiao.entity.Goods;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 查询订单数
     * @param uid
     * @return
     */
    @Select("SELECT COUNT(*) FROM `tb_order` where u_id=#{uid} and `evaluate`=0")
    int selectUserOrderCount(int uid);

    /**
     * 查询购物车商品数
     * @param cid
     * @return
     */
    @Select("SELECT COUNT(*) FROM cart where id=#{cid} and is_pay=0")
    int selectUserCartCount(int cid);

    /**
     * 查询gid商品是否有被uid用户收藏，有则返回1
     * @param uid
     * @param gid
     * @return
     */
    @Select("SELECT COUNT(*) FROM user_collect WHERE u_id=#{uid} AND g_id=#{gid}")
    int selectUserCollect(int uid,int gid);

    /**
     * 查询uid用户收藏的商品id集合
     * @param uid
     * @return
     */
    @Select("SELECT g_id FROM user_collect WHERE u_id=#{uid}")
    int[] selectCollectGid(int uid);

    /**
     * uid用户收藏gid商品
     * @param uid
     * @param gid
     * @return
     */
    @Insert("INSERT INTO `user_collect`(`u_id`, `g_id`) VALUES (#{uid}, #{gid})")
    int insertCollect(int uid,int gid);

    /**
     * uid用户取消收藏gid商品
     * @param uid
     * @param gid
     * @return
     */
    @Delete("DELETE FROM `user_collect` WHERE u_id=#{uid} AND g_id=#{gid}")
    int deleteCollect(int uid,int gid);

    /**
     * 查询uid用户收藏的商品id
     * @param uid
     * @return
     */
    @Select("SELECT g_id FROM user_collect WHERE u_id=#{uid}")
    List<Integer> selectCollect(int uid);

    /**
     * 根据商品id集合查询用户收藏的所有商品
     * @param ids
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            "goods.id,  " +
            "goods.title,  " +
            "goods.shop_name AS shopName,  " +
            "goods.price,  " +
            "goods.commodit_url AS commoditUrl " +
            "FROM " +
            "goods " +
            "WHERE id IN " +
            "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<Goods> selectCollectGoods(List<Integer> ids);
}
